<?php

namespace App\Jobs\QiPao\Gift;

use App\Jobs\Job;
use App\Services\Tools\IdempotentConsume;
use Illuminate\Support\Facades\Log;

class NewConnectionJob extends Job
{
    protected $data;
    const jobName = 'NewConnectionJob';
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(env('APP_ENV') != 'production'){
            Log::error('NewConnectionJob:'.json_encode(
                    [
                        'time' => getMillisecond(),
                        'data' => $this->data
                    ]
                ));
        }
        try {
            $idempotentConsumeServer =  new IdempotentConsume();
            $data = $this->data;
            //幂等消费
            if($idempotentConsumeServer->checkIdempotentRedisKey(self::jobName,$data['id'])){
                app('repo_new_connect')->userSendGiftToOtherUser($data);
            }
        } catch (\Exception $exception) {
            //失败之后删除幂等操作
            $idempotentConsumeServer->deleteIdempotentRedisKey(self::jobName,$data['id']);
            Log::error('ConnectionJob:'.$exception->getMessage());
        }
    }
    
   /* public function fail($exception=null )
    {
        Log::error('ConnectionJob:'.$exception->getMessage());
    }*/
}
